package com.neusoft.controller;

import com.neusoft.exception.FailToInitCredit;
import com.neusoft.exception.FailtocreateWallet;
import com.neusoft.po.CommonResult;
import com.neusoft.po.User;
import com.neusoft.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/UserController")
@RefreshScope       //开启动态刷新
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/getUserByIdByPass/{userId}/{password}")
    public CommonResult<User> getUserByIdByPass(@PathVariable("userId") String userId,
                                  @PathVariable("password") String password) throws Exception {
        User param=new User();
        param.setUserId(userId);
        param.setPassword(password);
        User user= userService.getUserByIdByPass(param);
        return new CommonResult<>(200,"success",user);
    }

    @GetMapping("/getUserById/{userId}")
    public CommonResult<Integer> getUserById(@PathVariable("userId") String userId) throws Exception {
        int result = userService.getUserById(userId);
        return new CommonResult<>(200,"success",result);
    }

    @PostMapping("/saveUser/{userId}/{password}/{userName}/{userSex}")
    public CommonResult<Integer> saveUser(@PathVariable("userId") String userId,
                        @PathVariable("password") String password,
                        @PathVariable("userName") String userName,
                        @PathVariable("userSex") Integer userSex) throws Exception {
        User param = new User();
        param.setUserId(userId);
        param.setPassword(password);
        param.setUserName(userName);
        param.setUserSex(userSex);
        try {
            int result = userService.saveUser(param);
            return new CommonResult<>(200,"success",result);
        }catch (FailToInitCredit e){
            return new CommonResult<>(403,"积分初始化不可用",null);
        }catch (FailtocreateWallet e){
            return new CommonResult<>(403,"创建钱包失败",null);
        }
    }

    @GetMapping("/isRider/{riderId}")
    public CommonResult<Integer> isRider(@PathVariable("riderId") String riderId){
        User param = new User();
        param.setUserId(riderId);
        return new CommonResult<>(200,"success", userService.isRider(param));
    }

    @PutMapping("/updateAsRider/{userId}")
    public CommonResult<Integer> updateAsRider(@PathVariable("userId") String userId){
        User param = new User();
        param.setUserId(userId);
        return new CommonResult<>(200,"success", userService.updateAsRider(param));
    }
}
